/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tbgb.resourcemanager.sprites;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import tbgb.resourcemanager.ImageResourceLoader;
import tbgb.resourcemanager.ResourceLoader;
import tbgb.resourcemanager.ResourceManager;
import tbgb.resourcemanager.sprites.SpriteDataBank.SpriteData;

/**
 *
 * @author LưuQuang
 */
public class SpriteLoader implements ResourceLoader{
    public static final Integer RESOURCE_KEY = new Integer("SpriteLoader".hashCode());
    
    private final ResourceManager resourceManager;
    private final SpriteDataBank dataBank;
    
    public SpriteLoader(ResourceManager rm, SpriteDataBank data)
    {
        this.resourceManager = rm;
        this.dataBank = data;
    }
    
    public Integer getResourceKey() {
        return RESOURCE_KEY;
    }

    public boolean isReuseResource() {
        return false;
    }

    public Object loadResource(String resourceName) {
        SpriteData sprData = this.dataBank.getSpriteData(resourceName);
        
        if (sprData != null)
        {
            Object imgObject = this.resourceManager.getResource(ImageResourceLoader.RESOURCE_KEY, sprData.imageName);
            if (imgObject instanceof Image)
            {
                Image spriteImage = (Image)imgObject;
                
                Sprite sprite = new Sprite(spriteImage, sprData.width,  sprData.height);
                sprite.setFrameSequence(sprData.frames);
                
                return sprite;
            }
        }
        
        return null;
    }
}